<?php

class ConfigConfiguration_Model extends Model {

    public function __construct(){
        parent::__construct();
    }
   
    public function getConfigurationDescriptors ($idConfiguration) {
        $configurationDescriptors = Array ();

        if (is_numeric ($idConfiguration)) {
            $query = "SELECT d.id, d.name, d.value, d.order ";
            $query .= "FROM configuration as c, configuration_descriptor as cd, descriptor as d ";
            $query .= "WHERE c.id = $idConfiguration AND ";
            $query .= "c.id = cd.id_configuration AND d.id = cd.id_descriptor ";
            $query .= "GROUP BY d.id";
            $result = $this->_db->select ($query);
            
            foreach ($result as $value) {
                $configurationDescriptors[] = Array (
                    "id" => $value->id,
                    "name" => $value->name,
                    "value" => $value->value,
                    "order" => $value->order
                );
            }
        }

        return $configurationDescriptors;
    }
    
    public function addConfigurationDescriptor ($idConfiguration, $name, $value, $order) {
        $params = Array();
        $params['name'] = $name;
        $params['value'] = $value;
        $params['order'] = $order;
        $id = $this->_db->insert ('descriptor', $params);
        
        $params2 = Array();
        $params2['id_configuration'] = $idConfiguration;
        $params2['id_descriptor'] = $id;
        $this->_db->insert ('configuration_descriptor', $params2);
        
        return true;
    }
    
    public function modifyConfigurationDescriptor ($id, $name, $value, $order) {
        $params = Array();
        $where = Array();
        $params['name'] = $name;
        $params['value'] = $value;
        $params['order'] = $order;
        $where['id'] = $id;
        $this->_db->update ('descriptor', $params, $where);
        
        return true;
    }
    
    public function removeConfigurationDescriptor ($id) {
        $this->_db->delete ('descriptor', 'id = ' . $id);
        
        return true;
    }
}

?>